/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* CheckTreeManager.java
*
* Created on December 29, 2007, 10:21 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package javautil.swing.tree;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;

/**
 *
 * @author stewari1
 */
class CheckTreeManager extends MouseAdapter implements TreeSelectionListener {

    /** Field description */
    private JTree tree = new JTree();

    /** Field description */
    private final int hotspot = new JCheckBox().getPreferredSize().width;

    /** Field description */
    private final CheckTreeSelectionModel selectionModel;

    /**
     * Constructs ...
     *
     * @param tree description
     */
    public CheckTreeManager(JTree tree) {

        this.tree      = tree;
        selectionModel = new CheckTreeSelectionModel(tree.getModel());

        tree.setCellRenderer(new CheckTreeCellRenderer(tree.getCellRenderer(), selectionModel));
        tree.addMouseListener(this);
        selectionModel.addTreeSelectionListener(this);
    }

    public void mouseClicked(MouseEvent me) {

        TreePath path = tree.getPathForLocation(me.getX(), me.getY());

        if (path == null) return;
        if (me.getX() > tree.getPathBounds(path).x + hotspot) return;

        boolean selected = selectionModel.isPathSelected(path, true);

        selectionModel.removeTreeSelectionListener(this);

        try {

            if (selected) selectionModel.removeSelectionPath(path);
            else selectionModel.addSelectionPath(path);

        } finally {

            selectionModel.addTreeSelectionListener(this);
            tree.treeDidChange();
        }
    }

    public CheckTreeSelectionModel getSelectionModel() {
        return selectionModel;
    }

    public void valueChanged(TreeSelectionEvent e) {
        tree.treeDidChange();
    }
}
